<?php
/*
 * @Descripttion: 
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-11-25 17:14:43
 * @LastEditTime : 2020-01-08 13:38:42
 */
namespace app\api\controller;

use think\Controller;

use app\admin\model\Message as MessageModel;
use app\admin\model\Searchword as SearchwordModel;
use app\admin\model\Keyword as KeywordModel;
use think\facade\Config;

class Message extends Controller
{
    public function add()
    {
        if(request()->isPost()){
            $param = input('post.');
            if($param['queren'] != '04f6ae5412e9c09efd8bce96b0970de3'){
                return reMsg(-1, '', '非法提交');
            }
            if (empty($param['dianhua'])) {
                return reMsg(-1, '', '请输入电话');
            }
            unset($param['queren']);
            $param_ms['dianhua'] = $param['dianhua'];
            $param_ms['xingming'] = $param['xingming'];
            $param_ms['neirong'] = $param['neirong'];
            $param_ms['ip'] = $param['ip'];
            $param_ms['ua'] = $param['ua'];
            $param_ms['url'] = $param['url'];
            $param_ms['ism'] = is_mobile($param_ms['ua']);
            if(!empty($param['usertrack'])){
                $usertrack = urldecode($param['usertrack']);
                unset($param['usertrack']);
                $ut = explode('|', $usertrack);
                $param_ms['from'] = $ut[0];
                $param_ms['inurl'] = $ut[1];
                if(strpos($ut[0], 'baidu.com') !== false && strpos($ut[0], 'eqid=') !== false) {
                    if(Config::get('system.referer_api') == 1){
                        $keyword = getKeyword2($ut[0]);
                    }else{
                        $arr = parse_url($ut[0]);
                        if(isset($arr['query'])){
                            parse_str($arr['query'], $query_arr);
                            if(isset($query_arr['eqid'])){
                                $keyword = getKeyword($query_arr['eqid']);
                            }
                        }
                    }
                    if(isset($keyword) && $keyword){
                        $param_ms['keyword'] = $keyword;
                    }
                };
                if (!empty($param_ms['keyword'])) {
                    $param_sw['keyword'] = $param_kw['keyword'] = $param_ms['keyword'];
                    $param_sw['ism'] = $param_kw['ism'] = $param_ms['ism'];
                    $keywordModel = new KeywordModel();
                    $res_kw = $keywordModel->addKeyword($param_kw);
                    $searchwordModel = new SearchwordModel();
                    $param_sw['kid'] = $param_ms['kid'] = $res_kw['data'];
                    $param_sw['ip'] = $param_ms['ip'];
                    $param_sw['inurl'] = $param_ms['inurl'];
                    $searchwordModel->addSearchword($param_sw);
                }
            }
            $model = new MessageModel();
            $res = $model->addMessage($param_ms);
            return json($res);
        }
    }
}
